<?php
include_once 'usermodel.php';
include_once 'util.php';
$page = new pager();
date_default_timezone_set('Asia/Ho_Chi_Minh');
//dang nhap
if($_POST["login"] == "login"){
    
    $count = count(login_system($_POST["username"], $_POST["password"]));
    $data = login_system($_POST["username"], $_POST["password"]);
    
    if($count > 0){        
        setcookie("username", $data[0]["username"], time()+3600*3600);
        setcookie("id",$data[0]["id"],time()+3600*3600);
        header("location: index.php");
    }else{
		header("location: login.php");
	}
    
}
$tvs = get_thong_tin_thanhvien_1($_COOKIE["id"]);
if(isset($_GET["keyword"])){
    search_form_ajax_user($_GET["keyword"]);
}
//lay danh sach thanh vien
if (isset($_GET["name"])) {
    $list_user = get_all_members_by_name($page, "?name={$_GET["name"]}&page=", $_GET["name"]);
} else if (isset($_GET["status"])) {
    $list_user = get_members_by_status($page, "?status={$_GET["status"]}&page=", $_GET["status"]);
} else if (!isset($_GET["name"]) && !isset($_GET["status"])) {
    $list_user = get_all_members($page, "?page=");
} else if (isset($_GET["status"]) && empty($_GET["status"])) {
    $list_user = get_all_members($page, "?page=");
}
//check username
if ($_GET["action"] == "checkName") {
    echo checkUsername($_GET['name']);
}
if($_GET["action"] == "xoa"){
    mysql_query("delete from post where user_id = '{$_POST["id"]}'");
    mysql_query("delete from comment where user_id = '{$_POST["id"]}'");
    echo xoa_thanhvien($_POST["id"]);
}
//them moi tai khoan
if (isset($_POST) && $_POST["checkSubmit"] == "submit") {
    if ((!empty($_FILES["avatar"])) && ($_FILES['avatar']['error'] == 0)) {    
        
        $arr_images = getFileInDirectory("../../data/avatar/");
        $filename = strtolower(basename($_FILES['avatar']['name']));
        $ext = substr($filename, strrpos($filename, '.') + 1);
        if (!empty($arr_images)) {
            foreach ($arr_images as $file) {                
                if ($file == $_FILES["avatar"]["name"]) {
                    $name = $file . "_" . md5(time()) . "." . $ext;
                    $folder = '../../data/avatar/' . $name;   
                    break;
                } else {
                    $name = $_FILES['avatar']['name'];
                    $folder = '../../data/avatar/' . $_FILES['avatar']['name'];                    
                }
            }
            move_uploaded_file($_FILES['avatar']['tmp_name'], $folder);
        } else {
            $name = $_FILES['avatar']['name'];
            $folder = '../../data/avatar/' . $_FILES['avatar']['name'];
            move_uploaded_file($_FILES['avatar']['tmp_name'], $folder);
        }
    } else {
        $name = "default_avatar.gif";
    }
    $ngaysinh = date("Y-m-d", strtotime("{$_POST["ngaysinh"]}"));
    $matkhau = md5($_POST["matkhau"]);
    $array = array(
        "id" => "''",
        "masv" => "'{$_POST["masv"]}'",
        "username" => "'{$_POST['taikhoan']}'",
        "password" => "'{$matkhau}'",
        "fullname" => "'{$_POST['fullname']}'",
        "ngaysinh" => "'{$ngaysinh}'",
        "gioitinh" => "'{$_POST['gioitinh']}'",
        "quequan" => "'{$_POST['quequan']}'",
        "noiohientai" => "'{$_POST['diachi']}'",
        "email" => "'{$_POST['email']}'",
        "sodienthoai" => "'{$_POST['sodienthoai']}'",
        "yahoo" => "'{$_POST['yahoo']}'",
        "skype" => "'{$_POST['skype']}'",
        "facebook" => "'{$_POST['facebook']}'",
        "description" => "'{$_POST["description"]}'",
        "avatar" => "'{$name}'",
        "status" => "1",
        "job" => "'{$_POST["job"]}'"
    );
    if (them_moi_thanhvien($array))
    {
        $kq = true;
    } else {
        $kq = false;
    }
}
if(isset($_COOKIE["username"])){
	$edit = get_thong_tin_thanhvien_1($_COOKIE["id"]);
}
//them moi thanh vien
//them moi tai khoan
if (isset($_POST) && $_POST["checkSubmit"] == "submit1") {
    //chmod("data/avatar/", 777);
    if ((!empty($_FILES["avatar"])) && ($_FILES['avatar']['error'] == 0)) {    
        
        $arr_images = getFileInDirectory("data/avatar/");
        $filename = strtolower(basename($_FILES['avatar']['name']));
        $ext = substr($filename, strrpos($filename, '.') + 1);
        if (!empty($arr_images)) {
            foreach ($arr_images as $file) {                
                if ($file == $_FILES["avatar"]["name"]) {
                    $name = $file . "_" . md5(time()) . "." . $ext;
                    $folder = 'data/avatar/' . $name;   
                    break;
                } else {
                    $name = $_FILES['avatar']['name'];
                    $folder = 'data/avatar/' . $_FILES['avatar']['name'];                    
                }
            }
            @move_uploaded_file($_FILES['avatar']['tmp_name'], $folder);
        } else {
            $name = $_FILES['avatar']['name'];
            $folder = 'data/avatar/' . $_FILES['avatar']['name'];
            @move_uploaded_file($_FILES['avatar']['tmp_name'], $folder);
        }
    } else {
        $name = "default_avatar.gif";
    }
    $ngaysinh = date("Y-m-d", strtotime("{$_POST["ngaysinh"]}")); 
    $matkhau = md5($_POST["password"]);
    $array = array(
        "id" => "''",
        "masv" => "'{$_POST["masv"]}'",
        "username" => "'{$_POST['username']}'",
        "password" => "'{$matkhau}'",
        "fullname" => "'{$_POST['fullname']}'",
        "ngaysinh" => "'{$ngaysinh}'",
        "gioitinh" => "'{$_POST['gioitinh']}'",
        "quequan" => "'{$_POST['quequan']}'",                
        "noiohientai" => "'{$_POST['noiohientai']}'",
        "email" => "'{$_POST['email']}'",
        "sodienthoai" => "'{$_POST['sodienthoai']}'",
        "yahoo" => "'{$_POST['yahoo']}'",
        "skype" => "'{$_POST['skype']}'",
        "facebook" => "'{$_POST['facebook']}'",
        "description" => "'{$_POST["description"]}'",
        "avatar" => "'{$name}'",
        "status" => "1",
        "job" => "'{$_POST["job"]}'"
    );
    $dem_masv = count(get_masv($_POST["masv"]));
    $dem_username = count(get_username($_POST["username"]));
    if($dem_masv <= 0 && $dem_username <= 0){
        if (them_moi_thanhvien($array))
        {
             echo "<script>alert('Đăng ký thành công!')</script>";
             echo "<script>window.location = 'login.php'</script>";
        } else {
            echo "<script>alert('Đăng ký thất bại!')</script>";
        }
    }else{
        echo "<script>alert('Hãy kiểm tra xem mã sinh viên của bạn hoặc username có trùng với ai không!')</script>";
    }
}
//thay doi status 
if ($_GET["action"] == "status") {
    echo thay_doi_trang_thai_thanhvien($_GET["stt"], $_GET["id"]);
}
//lay thogn tin thanh vien
if ($_GET["action"] == "edit") {
    $member = get_thong_tin_thanhvien($_GET["id"]);
}
//update thanh vien
if ($_POST["checkUpdate"] == "update1") {
    
    if ((!empty($_FILES["avatar"])) && ($_FILES['avatar']['error'] == 0)) {          
        $arr_images = getFileInDirectory("data/avatar/");
        $filename = strtolower(basename($_FILES['avatar']['name']));
        @unlink("data/avatar/".$_POST["getavatar"]);
        $ext = substr($filename, strrpos($filename, '.') + 1);
        if (!empty($arr_images)) {
            foreach ($arr_images as $file) {                
                if ($file == $_FILES["avatar"]["name"]) {
                    $name = $file . "_" . md5(time()) . "." . $ext;
                    $folder = 'data/avatar/' . $name;       
                    break;
                } else {
                    $name = $_FILES['avatar']['name'];
                    $folder = 'data/avatar/' . $_FILES['avatar']['name'];                    
                }
            }            
            @move_uploaded_file($_FILES['avatar']['tmp_name'], $folder);
        } else {
            $name = $_FILES['avatar']['name'];
            $folder = 'data/avatar/' . $_FILES['avatar']['name'];
            @move_uploaded_file($_FILES['avatar']['tmp_name'], $folder);
        }
    } else {        
        $name = $_POST["getavatar"];
    }
    $ngaysinh = date("Y-m-d", strtotime("{$_POST["ngaysinh"]}")); 
    $array = array(
        "masv" => "'{$_POST["masv"]}'",
        "username" => "'{$_POST['username']}'",        
        "fullname" => "'{$_POST['fullname']}'",
        "ngaysinh" => "'{$ngaysinh}'",
        "gioitinh" => "'{$_POST['gioitinh']}'",
        "quequan" => "'{$_POST['quequan']}'",
        "noiohientai" => "'{$_POST['noiohientai']}'",
        "email" => "'{$_POST['email']}'",
        "sodienthoai" => "'{$_POST['sodienthoai']}'",
        "yahoo" => "'{$_POST['yahoo']}'",
        "skype" => "'{$_POST['skype']}'",
        "facebook" => "'{$_POST['facebook']}'",
        "description" => "'{$_POST["description"]}'",
        "avatar" => "'{$name}'",
        "status" => "1",
        "job" => "'{$_POST["job"]}'"
    );            
    if (sua_thanhvien($array, $_COOKIE["id"]))
    {
         echo "<script>alert('Sửa thông tin cá nhân thành công!');</script>";
    } else {
        echo "<script>alert('Sửa thông tin cá nhân thất bại!');</script>";
    }
}
//thay doi password
if($_POST["checkChange"] == "change"){    
    $matkhau = md5($_POST["password_new"]);    
    $array = array(
        "password" => "'{$matkhau}'",             
    );    
    if (sua_thanhvien($array, $_COOKIE["id"]))
    {
        echo "<script>alert('Đổi thành công!');</script>";
    } else {
        echo "<script>alert('Đổi thất bại!');</script>";
    }
}
//check dung mat khau
if($_GET["action"] == "checkpass"){
	$dem = count(checkPass($_COOKIE["username"],$_POST["password"]));	
	if($dem > 0){
		echo true;
	}else{
		echo false;
	}
}
//lay danh sach den frontpage
if(!isset($_GET["uid"]) || $_GET["uid"] == ""){
    $list_user_f = get_member_to_frontpage();
}else if(isset($_GET["uid"]) && $_GET["uid"] != ""){
    $list_user_f = get_members_by_id($_GET["uid"]);
}
?>
